﻿namespace SharePointCSVImporter
{
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;

    public partial class ColumnRulesPopup : ChildWindow
    {
        #region Fields

        List<CSVParser.FieldMapping> _currentMappings;
        private CSVParser.FieldMapping _mapping;

        #endregion Fields

        #region Constructors

        public ColumnRulesPopup(ref CSVParser.FieldMapping mapping, List<CSVParser.FieldMapping> currentMappings)
        {
            InitializeComponent();
            _currentMappings = currentMappings;
            if (mapping != null)
            {
                _mapping = mapping;
                this.Title = mapping.DestinationColumn + " Transformation";
            }

            if (_mapping.Transformation == null)
            {
                _mapping.Transformation = new CSVParser.FieldTransformation();
            }
            else
            {
                SetInitialTransformType(_mapping.Transformation);
            }

            this.DataContext = _mapping;
        }

        #endregion Constructors

        #region Properties

        public CSVParser.FieldMapping Mapping
        {
            get { return _mapping; }
            set { _mapping = value; }
        }

        #endregion Properties

        #region Methods

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            _mapping.Transformation = null;
            this.DialogResult = false;
        }

        private void DisplayExternalLookupReplaceEditor()
        {
            this.RuleEditorControlBorder.Child = null;
            this.RuleEditorControlBorder.Child = new RuleEditors.ExternalLookupReplace(ref _mapping, _currentMappings);
        }

        private void DisplayLookupReplaceRuleEditor()
        {
            this.RuleEditorControlBorder.Child = null;
            this.RuleEditorControlBorder.Child = new RuleEditors.LookupReplace(ref _mapping);
        }

        private void DisplayStringReplaceRuleEditor()
        {
            this.RuleEditorControlBorder.Child = null;
            this.RuleEditorControlBorder.Child = new RuleEditors.StringReplaceRuleEditor(ref _mapping);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //
            if (this._mapping.Transformation.TransformationType == CSVParser.TransformationType.ExternalLookupReplace)
            {
                RuleEditors.ExternalLookupReplace editor = (RuleEditors.ExternalLookupReplace)this.RuleEditorControlBorder.Child;
                if (editor != null)
                {
                    this._mapping.Transformation = RuleEditors.ExternalLookupVM.ExternalLookupTranformParser.GetTransformValue(editor.ExternalLookupVM);
                }
            }
            this.DialogResult = true;
        }

        private void SetInitialTransformType(CSVParser.FieldTransformation transformation)
        {
            switch (transformation.TransformationType)
            {
                case CSVParser.TransformationType.StringReplace:
                    this.TransformTypeComboBox.SelectedIndex = 0;
                    DisplayStringReplaceRuleEditor();
                    break;
                case CSVParser.TransformationType.LookupReplace:
                    this.TransformTypeComboBox.SelectedIndex = 1;
                    DisplayLookupReplaceRuleEditor();
                    break;

                case CSVParser.TransformationType.ExternalLookupReplace:
                    this.TransformTypeComboBox.SelectedIndex = 2;
                    DisplayExternalLookupReplaceEditor();
                    break;

                default:
                    break;
            }
        }

        private void TransformTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TransformTypeComboBox.SelectedIndex > -1)
            {
                switch (TransformTypeComboBox.SelectedIndex)
                {
                    case 0:
                        //
                        this._mapping.Transformation.TransformationType = CSVParser.TransformationType.StringReplace;
                        DisplayStringReplaceRuleEditor();
                        break;
                    case 1:
                        this._mapping.Transformation.TransformationType = CSVParser.TransformationType.LookupReplace;
                        DisplayLookupReplaceRuleEditor();
                        break;
                    case 2:
                        this._mapping.Transformation.TransformationType = CSVParser.TransformationType.ExternalLookupReplace;
                        DisplayExternalLookupReplaceEditor();
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion Methods
    }
}